﻿using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;

using DTFight;

public class UIRoomMgr : MonoBehaviour
{
	static public UIRoomMgr Instance = null;
	
	public GameObject GridRoom;
	public UIRoom PfbRoom;

	void Awake() {
		Instance = this;
	}

	void OnDestroy()
	{
		Instance = null;
	}

	// Use this for initialization
	void Start ()
	{
		Screen.orientation = ScreenOrientation.Portrait;

		Init();
	}

	// Update is called once per frame
	void Update ()
	{

	}

	void Init()
	{
		if (GridRoom == null || PfbRoom == null)
			return;

		for (int i=0; i<100; ++i)
		{
			GameObject go = Instantiate(PfbRoom.gameObject) as GameObject;
			go.transform.SetParent(GridRoom.transform);
			go.transform.localPosition = Vector3.zero;
			go.transform.localScale = Vector3.one;

			UIRoom room = go.GetComponent<UIRoom>();
			if (room != null)
			{
				UIRoom.RoomData rd = new UIRoom.RoomData();
				rd.RoomId = i+1;
				rd.RoomName = "房间" + rd.RoomId.ToString();
				room.InstData = rd;
			}
		}

		VerticalLayoutGroup vlg = GridRoom.GetComponent<VerticalLayoutGroup>();
		if (vlg != null)
		{
			RectTransform rt = GridRoom.GetComponent<RectTransform>();
			//rt.offsetMax = new Vector2(0, vlg.spacing * GridRoom.transform.childCount);
			float h = vlg.spacing * GridRoom.transform.childCount;
			rt.localPosition = new Vector3(0, -h/2, 0);
			rt.sizeDelta = new Vector2(0,  h);

		}
	}

	public void EnterRoom(int id)
	{
		RoomMsg.Instance().SubmitEnterRoom(UILogin.CurPlayer.Id, id);
//		RoomMsg.Instance().SubmitEnterRoom(DateTime.Now.ToString(), id);
	}
}

